import 'package:flutter/material.dart';
import 'package:flutter_smart_heating/ui/ui.dart';

import 'package:get/get.dart';
import 'package:url_launcher/url_launcher.dart';

import 'http.dart';
import 'local_storage.dart';

//检测新版本
Future checkAppVersion({
  bool showLoading = false,
  bool showMsg = false,
  bool isLaunch = false,
}) async {
  var res = await Http.instance.post('/plugin/app_manager/update/check', data: {
    'is_launch': isLaunch ? 1 : 0,
  });
  if (res.code == 1) {
    if (!showLoading && res.data['forceUpdate'] != true) {
      var ignoreVersion =
          LocalStorage.instance.getItem<String>('ignore.version');
      if (ignoreVersion == res.data['version']) {
        return;
      }
    }
    Get.dialog(
      DefaultTextStyle(
        style: const TextStyle(
          decoration: TextDecoration.none,
          color: Colors.black,
        ),
        child: Center(
          child: Container(
            constraints: const BoxConstraints(maxWidth: 320),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Stack(
                  children: [
                    SizedBox(
                      width: double.infinity,
                      child: Image.asset(
                        'res/images/update.png',
                        width: double.infinity,
                      ),
                    ),
                    Positioned(
                      top: 115,
                      left: 20,
                      child: Text(
                        res.data['version'],
                        style: TextStyle(
                          color: Colors.white.withOpacity(0.8),
                          fontSize: 20,
                          decorationColor: Colors.white,
                        ),
                      ),
                    )
                  ],
                ),
                Container(
                  width: double.infinity,
                  padding: const EdgeInsets.symmetric(
                    horizontal: 15,
                  ),
                  decoration: const BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.vertical(
                      bottom: Radius.circular(10),
                    ),
                  ),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.start,
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Container(
                        constraints: const BoxConstraints(
                          maxHeight: 110,
                          minHeight: 30,
                        ),
                        margin: const EdgeInsets.only(
                          bottom: 20,
                        ),
                        child: SingleChildScrollView(
                          padding: const EdgeInsets.symmetric(
                            horizontal: 10,
                          ),
                          child: SizedBox(
                            width: double.infinity,
                            child: Text(
                              res.data['intro'],
                              style: const TextStyle(height: 1.6, fontSize: 16),
                            ),
                          ),
                        ),
                      ),
                      Container(
                        height: 40,
                        width: double.infinity,
                        margin: const EdgeInsets.symmetric(horizontal: 10),
                        child: UiButton(
                          padding: EdgeInsets.zero,
                          onPressed: () async {
                            var result = await launchUrl(
                              Uri.parse(res.data['url']),
                              mode: LaunchMode.externalApplication,
                            );
                            if (!result) {
                              Ui.toast('Could not launch Url ');
                              Get.back();
                            }
                          },
                          child: Text(
                            res.data['confirmButton'] ?? '立即升级',
                            style: const TextStyle(fontSize: 16),
                          ),
                        ),
                      ),
                      if (!res.data['forceUpdate'])
                        Container(
                          margin: const EdgeInsets.symmetric(horizontal: 10),
                          width: double.infinity,
                          child: TextButton(
                            onPressed: () {
                              LocalStorage.instance.setItem(
                                  'ignore.version', res.data['version']);
                              Get.back();
                            },
                            child: Text(
                              res.data['cancelButton'] ?? '忽略此次升级',
                            ),
                          ),
                        ),
                      if (res.data['forceUpdate'])
                        const SizedBox(
                          height: 25,
                        ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
      barrierDismissible: false,
      useSafeArea: false,
    );
  } else if (showMsg) {
    Ui.toast(res.msg);
  }
}
